package spec.zyh;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

//@WebServlet(name = "ViewServlet")
public class ViewServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            String t = request.getParameter("username");
            String c = request.getParameter("password");
//            String urL="jdbc:mysql://Localhost:3306/1613580003?characterEncoding=utf8";
//            String name="root";
//            String password="123456";
//            Class.forName("com.mysql.jdbc.Driver");
//            Connection com= null;
//            com = DriverManager.getConnection(urL,name,password);
//            Statement s=com.createStatement();
            DatabaseConnetion dbc=new DatabaseConnetion();
            Connection com=dbc.getConnection();
            Statement s = null;
            s = com.createStatement();
            String id=request.getParameter("id");
            response.setContentType("text/html");
            ResultSet rs=s.executeQuery("select * from t_news where id="+id);
            PrintWriter out1 = response.getWriter();
            String h=" ";
                    h +="<!DOCTYPE html>\n" +
                    "<html lang=\"en\">\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <title>查看</title>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "<form action=\"l\" method=\"post\">\n" +
                    "    <input type=\"button\" value=\"返回上一页\" id=\"f\" style=\"color:gray; font-size: 18px;\">\n" +
                    "</form>\n" +
                    "<script>\n" +
                    "    var f= document.getElementById(\"f\");\n" +
                    "    f.onclick=function () {\n" +
                    "        window.open('login.html');\n" +
                    "    }\n" +
                    "</script>\n" ;
                    while (rs.next()) {
                        h += "<h1><p align=\"center\">" + rs.getString("title") + "</h1></p>";
                        h += "<h6><p align=\"center\">发布时间：" + rs.getString("publish-time") + "</h6></p>";
                        h += "<blockquote>\n";
                        h += "    <p align=\"justify\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + rs.getString("content") + "</p>";
                        h += "</blockquote>\n" ;
                    }
                    h += "</body>\n" +
                    "</html>";
            out1.println(h);
            dbc.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    }

